package org.jeecgframework.core.util.clean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jeecg.business.entity.site.SiteEntity;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;
import org.jeecgframework.core.util.icp.IcpUtil;

public class ListHtmlCleanUtil{
	
	public static List<HtmlParseResult> listInfo(String href , SiteEntity site , String keyword){
		return listInfo(href, site, keyword, null , 0);
	}
	
	public static List<HtmlParseResult> listInfo4Html(String html , SiteEntity site , String keyword , String href){
		TagNode node = null;
		List<HtmlParseResult> list = new ArrayList<HtmlParseResult>();
		String lyurl = null;
		HtmlParseResult result = null;
		try{
			HtmlCleaner cleaner = new HtmlCleaner();
			node = cleaner.clean(html);
			/**
			 * 下一页
			 */
			String nextPage = null;
			nextPage = HtmlCleanBaseUtil.getAttributeValue(node, site.getLbxyy() , "href");
			Object[] ns=node.evaluateXPath(site.getLbtq());
			if(ns != null){
				TagNode childNode = null;
				TagNode temp = null;
				Object [] objs = null;
				TagNode ob = null;
				for(Object obj : ns){
					try{
						result = new HtmlParseResult();
						result.setCharset(site.getWyzfj());
						result.setType(site.getJclx());
						result.setSite(site.getMc());
						result.setKeyword(keyword);
						result.setCjzd(href);
						
						childNode = (TagNode)obj;
						
	//					System.out.println(HtmlCleanBaseUtil.getAllContent(cleaner, childNode));
						/**
						 * 标题
						 */
						String title = null;
						if(StringUtil.isNotEmpty(site.getZwbt())){
							title = HtmlCleanBaseUtil.getTagContent(childNode, site.getZwbt());
						}else{
							title = HtmlCleanBaseUtil.getTitle(node);
						}
						result.setTitle(title);
						
						if("2".equals(site.getSjhqfs())){		//列表获取时间	-- 获取时间
							/**
							 * 时间
							 */
							String time = null;
							String timeXpath = null;
							String timeReg = null;
							if(StringUtil.isNotEmpty(site.getZwsj())){
								if(site.getZwsj().indexOf("@|@")>0){
									String [] array = site.getZwsj().split("@\\|@");
									timeXpath = array[0];
									timeReg = array[1];
									time = HtmlCleanBaseUtil.getTagContent(childNode, timeXpath);
									Pattern p = Pattern.compile(timeReg);
									Matcher m = p.matcher(time);
									if(m.find()) 
										time = m.group();
								}else{
									time = HtmlCleanBaseUtil.getTagContent(childNode, site.getZwsj());
								}
								if(StringUtil.isNotEmpty(time)){
									Date eventdate = DataUtils.parseDate(time);
									result.setEventDate(eventdate);
								}
							}
						}
						
						/**
						 * 正文
						 */
						String zwnrXpath = "//body";
						if(StringUtil.isNotEmpty(site.getZwnr())) zwnrXpath = site.getZwnr();
	//					childNode.removeAllChildren();
						//objs = childNode.evaluateXPath(zwnrXpath);
						//ob = (TagNode)objs[0];
						
						/**
						 * 正文连接
						 */
						if(StringUtil.isNotEmpty(site.getZwlj())){
							lyurl = HtmlCleanBaseUtil.getAttributeValue(childNode, site.getZwlj() , "href");
							result.setUrl(lyurl);
							result.setLy(lyurl);
						}
						/**
						 * 去掉第一个和最后一个
						 */
						String content = null;
						
						/**
						 * 下一页
						 */
						if(StringUtil.isNotEmpty(nextPage)){
							result.setNextPageUrl(nextPage);
						}
						
						result.setBodycontent(content);
						
						list.add(result);
					}catch(Exception e){
						e.printStackTrace();
						System.out.println("抓取失败："+href);
					}
				}
			}
			
		}catch(Exception e){
			System.out.println("抓取失败："+href);
			e.printStackTrace();
			return null;
		}
		
		return list;
	}
	
	
	public static List<HtmlParseResult> listInfo(String href , SiteEntity site , String keyword , String ip , int port){
		
		String html = null;
		if(ip == null)
			html = HttpClientUtil.getSourceFromUrl(href, site.getWyzfj(), null);
		else 
			html = HttpClientUtil.getSourceFromUrl(href, site.getWyzfj(), null , ip , port);
		
		return listInfo4Html(html, site, keyword, href);
	}
	
	public static void main(String[] args) {
		System.out.println(new Date().getTime());
		//1445931179
		//1445931368

	}
}
